<!--
 * @Author: 颜礼田 2564217790@qq.com
 * @Date: 2024-08-26 10:41:26
 * @LastEditors: MrDotYan 2564217790@qq.com
 * @LastEditTime: 2024-09-13 20:50:31
 * @FilePath: /turo-repo-enterprise/apps/fuck-enterprise-admin/src/views/system/roleMange/index.vue
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
-->
<template>
  <div class="table-box">
    <ProTable ref="proTableRef" :columns="columns" :request-api="fetchRoleList">
      <template #tableHeader>
        <el-button type="primary" v-auth="'System.Role.AddRole'" @click="open()"> 添加 </el-button>
      </template>
    </ProTable>
    <Edit ref="editRef" @success="refresh"></Edit>
  </div>
</template>

<script setup lang="tsx" name="roleManage">
import ProTable from "@/components/ProTable/index.vue";
import { ColumnProps, ProTableInstance } from "@/components/ProTable/interface";
import { fetchRoleList, Role, removeRole } from "@/api/modules/roles";
import Edit from "./components/edit.vue";
import { ref } from "vue";
import { ElMessageBox, ElNotification } from "element-plus";
import { ResultEnum } from "@/enums/httpEnum";

const proTableRef = ref<ProTableInstance>();

function remove(row?: Role) {
  ElMessageBox.confirm("提示", { type: "error", message: "是否删除当前数据？" })
    .then(() => {
      if (row?.id) {
        removeRole({ id: row?.id }).then(r => {
          const { code, message } = r;
          if (code == ResultEnum.SUCCESS) {
            ElNotification.success("删除成功！");
            refresh();
          } else {
            ElNotification.error(message);
          }
        });
      }
    })
    .catch(err => {
      console.error(err);
    });
}

function edit(row?: Role) {
  open(row);
}

const columns: Array<ColumnProps> = [
  { type: "index", label: "序号", fixed: "left", width: 80 },
  { label: "角色名称", prop: "roleName", search: { el: "input" } },
  {
    label: "操作",
    prop: "action",
    render: ({ row }) => {
      return (
        <>
          <el-button link type="primary" v-auth="System.Role.EditRole" onClick={() => edit(row)}>
            编辑
          </el-button>
          <el-button link type="danger" v-auth="System.Role.RemoveRole" onClick={() => remove(row)}>
            删除
          </el-button>
        </>
      );
    }
  }
];

const editRef = ref();
function open(row?: Role) {
  editRef.value?.open(row);
}

function refresh() {
  proTableRef.value?.getTableList();
}
</script>
